package com.hxkj.cst.chengshuitong

import android.content.Context
import android.content.SharedPreferences
import android.os.IBinder
import android.support.annotation.StringRes
import android.view.View
import android.view.inputmethod.InputMethodManager
import android.widget.Toast

/**
 * 作者： Luoyang on 2017/5/25.
 * 描述：
 */

/*
* @param key 存数据的key值
* @param value 数据
* 存放数据到Shared中
* */
fun Context.setDataToShared(key: String, value: Any) {
    val shared: SharedPreferences = getSharedPreferences("cst", Context.MODE_PRIVATE)
    val editor: SharedPreferences.Editor = shared.edit()
    when (value) {
        is Int -> editor.putInt(key, value)
        is String -> editor.putString(key, value)
        is Long -> editor.putLong(key, value)
        is Float -> editor.putFloat(key, value)
        is Boolean -> editor.putBoolean(key, value)
    }
    editor.apply()
}

/*
* @param key  获取对象的key值
* @param default 默认值
* 从Shared中取数据
* */
fun Context.getDataFromShared(key: String, default: Any): Any {
    val shared: SharedPreferences = getSharedPreferences("cst", Context.MODE_PRIVATE)
    when (default) {
        is Int -> return shared.getInt(key, default)
        is String -> return shared.getString(key, default)
        is Long -> return shared.getLong(key, default)
        is Float -> return shared.getFloat(key, default)
        is Boolean -> return shared.getBoolean(key, default)
    }
    return ""
}

fun Context.toast(@StringRes stringRes: Int, duration: Int? = Toast.LENGTH_LONG) =
        Toast.makeText(this, stringRes, duration!!).show()

fun Context.toast(stringRes: String, duration: Int? = Toast.LENGTH_LONG) =
        Toast.makeText(this, stringRes, duration!!).show()

fun Context.closeInputMethod(iBinder: IBinder) {
    val inputMethodManager: InputMethodManager = getSystemService(Context.INPUT_METHOD_SERVICE)
            as InputMethodManager
    if (inputMethodManager.isActive)
        inputMethodManager.hideSoftInputFromWindow(iBinder, 0)
}

fun Context.openInputMethod(view: View) {
    val inputMethodManager: InputMethodManager = getSystemService(Context.INPUT_METHOD_SERVICE)
            as InputMethodManager
    if (!inputMethodManager.isActive)
        inputMethodManager.showSoftInput(view, 0)
}

const val CURRENT_CAPTURE_INDEX: Int = 0




